//https://github.com/rollup/plugins/blob/master/packages/alias/src/index.ts
//
//https://github.com/rollup/plugins/blob/master/packages/babel/src/index.js

export default function alias(options){
  console.log('alias received options:', options);
  //alias received options: { entries: { a: './a' } } //就是我们rollup.config.js里传的

  //插件其实是一个对象
  //plugins:[ alias() ]
  return {
    //name 非强制
    name: 'alias',

    //https://rollupjs.org/guide/en/#options
    //rollup里第一个执行的hook
    //This is the first hook of the build phase
    //这是唯一一个无法访问大多数插件上下文实用程序功能的钩子，因为它在 rollup 完全配置之前运行。
    //So该钩子一般是用作本插件的options的预处理工作
    options(InputOptions){
      console.log('options hook --- --- ---');
    },

    //rollup里的hook --- buildStart(inputOptions)
    //在真正构建代码之前 可在此处对整体的rollup配置进行修改 （可以拿到所有插件的配置）
    buildStart(inputOptions){
      console.log('buildStart --- inputOptions:', inputOptions); // 这次rollup编译的整体配置 (包含我们rollup.config.jsi配置的信息)
/*
buildStart --- inputOptions: {
  acorn: {
    allowAwaitOutsideFunction: true,
    ecmaVersion: 'latest',
    preserveParens: false,
    sourceType: 'module'
  },
  acornInjectPlugins: [],
  context: 'undefined',
  experimentalCacheExpiry: 10,
  external: [Function (anonymous)],
  inlineDynamicImports: undefined,
  input: [ 'index.js' ],
  makeAbsoluteExternalsRelative: true,
  manualChunks: undefined,
  maxParallelFileReads: 20,
  moduleContext: [Function (anonymous)],
  onwarn: [Function (anonymous)],
  perf: false,
  plugins: [
    { name: 'json', transform: [Function: transform] },
    { name: 'alias', buildStart: [Function: buildStart] },
    {
      load: [Function: load],
      name: 'stdin',
      resolveId: [Function: resolveId]
    }
  ],
  preserveEntrySignatures: 'strict',
  preserveModules: undefined,
  preserveSymlinks: false,
  shimMissingExports: false,
  strictDeprecations: false,
  treeshake: {
    annotations: true,
    correctVarValueBeforeDeclaration: false,
    moduleSideEffects: [Function (anonymous)],
    propertyReadSideEffects: true,
    tryCatchDeoptimization: true,
    unknownGlobalSideEffects: true
  }
}
*/
    }

    //https://rollupjs.org/guide/en/#resolveid
    //Type: (source: string, importer: string | undefined, options: {isEntry: boolean, custom?: {[plugin: string]: any}) => string | false | null | {id: string, external?: boolean | "relative" | "absolute", moduleSideEffects?: boolean | "no-treeshake" | null, syntheticNamedExports?: boolean | string | null, meta?: {[plugin: string]: any} | null}
    //rollup里的hook --- resolveId
    //可在此处替换一个文件的解析路径
    , resolveId(importee/*import xxx from yyy 的yyy*/, importer/*调用import的文件*/) {
      console.log('resolveId importee:', importee);
      console.log('resolveId importer:', importer);
      /*
        resolveId importee: index.js
        resolveId importer: undefined //说明是rollup入口文件
        resolveId importee: a  //← import {doConsole} from 'a'; 的 a
        resolveId importer: F:\code\js_workplace\tutorial\rollup_tutorial\03.plugin\01.alias\index.js
      */

      // return {id: updatedId}
    }

    //https://rollupjs.org/guide/en/#load
    //resolveId之后 如何去加载代码的实现
    //因为有这个文件可能不存在于硬盘上 它是虚拟的
    //我们可以通过http 或者 从内存中撒的去获取到
    // ,load(id){}

    // (code: string, id: string) => string | null | {code?: string, map?: string | SourceMap, ast? : ESTree.Program, moduleSideEffects?: boolean | "no-treeshake" | null, syntheticNamedExports?: boolean | string | null, meta?: {[plugin: string]: any} | null}
    //Can be used to transform individual modules
    ,transform(code,filename){

    }

    //https://rollupjs.org/guide/en/#resolvedynamicimport
    //import().then
    // ,resolve

    //有可能在我们transform的阶段 把一部分的其它代码编译成了我们想要去replace的代码
    //这部分代码我们不可能在transform里再去replace一遍了
    //这个时候我们就可以到↓renderChunk里再去replace
    //https://github.com/rollup/plugins/blob/8fadc64c679643569239509041a24a9516baf340/packages/replace/src/index.js#L94
    ,renderChunk(){}
  }
}
